<?php
/**
 * qMySQLConnection is wrapper class for standard mysql connection identifiers. Do not try to construct directly, use qMySQLFactory.
 */
class qMySQLConnection
{
    /** Standard MySQL connection identifier. */
    private $link;
    
    /** Error message. */
    private $error;
    
    /** Constructor. Do not call this directly, use qMySQLFactory.
     * @param string $host database host
     * @param string $user database username
     * @param string $pass database password
     * @throws Exception if not called from qMySQLFactory
     */
    function __construct($host, $user, $pass)
    {
        // check if it's called from qMySQLFactory, if not throw Exception
        $backtrace = debug_backtrace();
        if ($backtrace[1]['class'] != "qMySQLFactory") throw new Exception();
        
        // connect
        $this->link = @mysql_connect($host, $user, $pass);
        
        // if error occured, update $error
        if ($errno = mysql_errno() && $error = mysql_error())
        {
            $this->error = "MySQL Error #$errno: $error";
        }
    }
    
    /**
     * Selects working database.
     * @param string $db database to be selected
     */
    public function SelectDatabase($db)
    {
        @mysql_select_db($db, $this->link);

        // if error occured, update $error
        if ($errno = mysql_errno() && $error = mysql_error())
        {
            $this->error = "MySQL Error #$errno: $error";
        }
    }
    
    /** 
     * Closes link.
     */
    public function Disconnect()
    {
        @mysql_close($this->link);

        // if error occured, update $error
        if ($errno = mysql_errno() && $error = mysql_error())
        {
            $this->error = "MySQL Error #$errno: $error";
        }
    }
    
    /**
     * Gets list of tables in selected database.
     * @return qMySQLQuery with results of SHOW TABLES
     */
    public function GetTables()
    {
        return new qMySQLQuery("SHOW TABLES", $this->link);
    }
    
    /**
     * Loads table into qMySQLTable.
     * @param string $name name of table.
     * @param string $conds conditions to be placed in query
     * @return qMySQLTable representing loaded table.
     */
    public function GetTable($name)
    {
        // it's a query builder; will be implemented soon (tm)
        return new qMySQLQuerer($name);
    }
    
    /**
     * Performs database query.
     * @param string $query query to be performed
     * @return qMySQLQuery / false
     */
    public function Query($query)
    {
        $query = new qMySQLQuery($query, $this->link);
    
        // if error occured, update $error
        if ($errno = mysql_errno() && $error = mysql_error())
        {
            $this->error = "MySQL Error #$errno: $error";
            return false;
        }
        
        return $query;
    }
    
    /** 
     * Returns error string.
     * @return Error string.
     */
    public function Error()
    {
        $error = $this->error;
        $this->error = '';
        return error;
    }
}